/**
 * Copyright (C), 2020-2021
 * FileName: PayApiImpl
 * Author:   86189
 * Date:     2021/3/8 21:24
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.itheima.wallet.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.wallet.mapper.AccountMapper;
import com.itheima.wallet.mapper.TradeMapper;
import com.itheima.wallet.pojo.Account;
import com.itheima.wallet.pojo.Trade;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author 86189
 * @create 2021/3/8
 * @since 1.0.0
 */
@Service(version = "1.0.0")
public class PayApiImpl implements PayApi {

    @Autowired
    private TradeMapper tradeMapper;

    @Autowired
    private AccountMapper accountMapper;

    /**
     * 充值
     *
     * @param accountId
     * @param money
     * @return
     */
    @Override
    public Boolean pay(Long accountId, Double money) {
        //查询余额
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId);
        Account account = accountMapper.selectOne(queryWrapper);

        if (account == null) {
            return false;
        }
        //修改余额
        account.setBalance(account.getBalance() + money);
        account.setUpdateTime(new Date());
        QueryWrapper<Account> query = new QueryWrapper();
        query.eq("account_id",accountId);
        accountMapper.update(account,query);

        //记录流水
        Trade trade = new Trade();
        trade.setAccountId(accountId);
        trade.setTradeMoney(money);
        trade.setTradeTime(new Date());
        trade.setTradeType(1);
        tradeMapper.insert(trade);
        return true;
    }

    /**
     * 提现
     *
     * @param accountId
     * @param money
     * @return
     */
    @Override
    public Boolean reduce(Long accountId, Double money) {
        //查询余额
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId);
        Account account = accountMapper.selectOne(queryWrapper);

        if (account == null) {
            return false;
        }
        //修改余额
        if ((account.getBalance() - money) < 0) {
            return false;
        }
        account.setBalance(account.getBalance() - money);
        account.setUpdateTime(new Date());
        QueryWrapper<Account> query = new QueryWrapper();
        query.eq("account_id",accountId);
        accountMapper.update(account,query);

        //记录流水
        Trade trade = new Trade();
        trade.setAccountId(accountId);
        trade.setTradeMoney(money);
        trade.setTradeTime(new Date());
        trade.setTradeType(2);
        tradeMapper.insert(trade);
        return true;
    }

    /**
     * 查看流水
     *
     * @param accountId
     * @return
     */
    @Override
    public List<Trade> listTrade(Long accountId) {

        QueryWrapper<Trade> queryWrapper = new QueryWrapper();
        queryWrapper.eq("account_id",accountId);
        List<Trade> tradeList = tradeMapper.selectList(queryWrapper);

        return tradeList;
    }

    /**
     * 查看账户信息
     *
     * @param accountId
     * @return
     */
    @Override
    public Account listAccount(Long accountId) {

        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId);
        Account account = accountMapper.selectOne(queryWrapper);

        return account;
    }
}
